package rmi;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;


/*
*   服务器至少暴漏如下RPC/RMI接口：
*       bool add(Book b)  添加一个书籍对象
*       Book queryByID(int bookID)  查询指定ID的书籍对象
*       BookList queryByName(String name) 按书名查询书籍列表
*       bool delete(int bookID) 删除指定ID号的书籍对象
* 
*/

public class client {
	public static void main(String[] args) throws RemoteException, NotBoundException {
		String serverName = "calculator";
		String serverIP = "127.0.0.1";
		int port = 1999;
		Registry registry = LocateRegistry.getRegistry(serverIP, port);
		BookManage de = (BookManage)registry.lookup(serverName);
		Scanner in = new Scanner(System.in);
		System.out.println("=============================================");
		System.out.println("\t\tBOOK MANAGEMENT:");
		System.out.println("help                    -> get help\n"
						 + "exit                    -> to exit\n"
						 + "add [bookID] [bookName] -> add book\n"
						 + "queryID [bookID]        -> query by ID \n"
						 + "queryName [bookName]    -> query by name\n"
						 + "delete [bookID]         -> delete book");
		
		System.out.println("(add, queryID, queryName, delete, help, exit)");
		System.out.println("=============================================");
		while(true) {
			String[] paras = in.nextLine().split(" ");
			switch(paras[0]) {
				case "add":{
					boolean status = de.add(new Book(Integer.valueOf(paras[1]), paras[2]));
					System.out.println("status :"+status);
					break;
				}case "queryID":{
					Book book = de.queryById(Integer.valueOf(paras[1]));
					if(book != null) book.disp();
					else System.out.println("status: none ID error");
					break;
				}case "queryName":{					
					BookList booklist = de.queryByName(paras[1]);
					booklist.disp();
					break;
				}case "delete":{
					boolean status = de.delete(Integer.valueOf(paras[1]));
					System.out.println("status: "+status);
					break;
				}case "exit":{
					System.exit(0);
				}case "help":{
					System.out.println("help                    -> get help\n"
									 + "exit                    -> to exit\n"
									 + "add [bookID] [bookName] -> add book\n"
									 + "queryID [bookID]        -> query by ID \n"
									 + "queryName [bookName]    -> query by name\n"
									 + "delete [bookID]         -> delete book");
					break;
				}default:{
					System.out.println("invalid parameters/none info");
				}
					
				
			
			}
		}
	}
}
